package com.example.demo.tx.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.example.demo.tx.entity.Citymap;
import com.example.demo.tx.repository.CitymapRepository;
import com.example.demo.tx.service.TxService;
import com.example.demo.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @Author: zjq
 * @Date: create in 2023/6/15 15:44
 */
@Api(tags = {"微信接口"})
@RestController
@RequestMapping("/tx")
public class txController {

    @Autowired
    private TxService txService;
    @Autowired
    private CitymapRepository citymapRepository;

    /**
     * 验证微信签名
     */
    @GetMapping("/index")
    public String gettoken(String signature, String timestamp, String nonce, String echostr) {
        boolean checkSignature = txService.checkSignature(signature, timestamp, nonce);
        if (checkSignature == true) {
            return echostr;
        } else {
            return null;
        }
    }

    @ApiOperation(value = "天气查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "adcode", value = "城市的adcode", required = true, dataType = "String"),
            @ApiImplicitParam(name = "extensions", value = "base:返回实况天气  all:返回预报天气", required = false, dataType = "String")})
    @GetMapping("/getweather")
    public Object getweather(String adcode, String extensions) {
        return txService.getweather(adcode, extensions);
    }

    @ApiOperation(value = "根据IP定位查询天气情况")
    @GetMapping("/getweatherByIp")
    public Object getIp() {
        Map<String, Object> map = JSON.parseObject(txService.getIp().toString(), Map.class);
        if (map.get("status").equals("1")) {
            return txService.getweather(map.get("adcode").toString(), "base");
        } else {
            return ResultUtil.info(-1, map.get("info").toString());
        }
    }

    /**
     * 目前是最快的速度
     * 1.数据库配置文件开启&rewriteBatchedStatements=true批量保存
     * 2.jpa的主键政策不能是自增---目前使用jpa-uuid
     * jpa自带的saveAll方法一条一条执行需要先查询，再保存
     *
     * @param file
     * @throws IOException
     */
    @ApiOperation(value = "高德城市编码表写入数据库")
    @PostMapping("import")
    @ResponseBody
    public void uploadFileData(MultipartFile file) throws IOException {
        Long start = System.currentTimeMillis();
        // 写法1：JDK8+ ,不用额外写一个DemoDataListener
        // since: 3.0.0-beta1
        EasyExcel.read(file.getInputStream(), Citymap.class, new PageReadListener<Citymap>(dataList -> {
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            citymapRepository.batchSave(dataList);
        })).sheet().doRead();

        Long end = System.currentTimeMillis();
        System.out.println("开始时间" + start);
        System.out.println("结束时间" + end);
        Long totaltime = end - start;
        System.out.println("花费时间" + totaltime);

    }

    @ApiOperation(value = "模糊搜索城市编码表")
    @GetMapping("/getcitycode")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "城市名称", required = true, dataType = "String")})
    public Object encryptResponse(String name) {
        return ResultUtil.success("data", citymapRepository.getSimilarByName('%' + name + '%'));
    }

}
